﻿#include "CPdfRptDataPkg.h"
#include "CGlobal.h"
#include "CSqlOper.h"
#include "Public.h"

CPdfRptDataPkg::CPdfRptDataPkg(QObject *parent)
	: QObject(parent)
{

}

CPdfRptDataPkg::~CPdfRptDataPkg()
{

}

bool CPdfRptDataPkg::pkgRestPdfRptData(QDateTime dtSearchStartTime, QDateTime dtSearchEndTime, QString sPdfFolderPath, QString sHttpPrefix, CAllPdfRptDataInfo &dataInfo, QString &sErrMsg)
{
	//声振温
	QDateTime dtCurTime = QDateTime::currentDateTime();//当前系统时间
	QString sCurrTime = dtCurTime.toString("yyyyMMddhhmmss");
	//创建文件夹
	QString sFolderTemp = sPdfFolderPath + "Result/" + "Sese" + "/" + QDateTime::currentDateTime().toString("yyyyMM") + "/";//文件夹+Tear+年月
	QDir folder(sFolderTemp);// 创建QDir对象
	if (!folder.exists())
	{
		// 文件夹不存在，则创建
		folder.mkpath(".");
	}
	//输出路径
	QString sOutPath = sFolderTemp + sCurrTime + A2T("声振温检测报告.pdf");
	dataInfo.extraDataInfo.m_sOutputPdfFolder = sOutPath;

	QString sMsgSese = "";
	QVector<QSqlRecord> vecRecordSese;
	CSqlOper::instance().selSeseResultInfo(vecRecordSese, dtSearchStartTime.toString("yyyy/MM/dd hh:mm:ss"), dtSearchEndTime.toString("yyyy/MM/dd hh:mm:ss"), sMsgSese);

	//dataInfo.seseDataInfo.m_nAlmCount = vecRecordSese.size();
	dataInfo.extraDataInfo.m_sComment = A2T("空");
	dataInfo.extraDataInfo.m_sDetTime = dtSearchEndTime.toString("yyyy-MM-dd");
	dataInfo.extraDataInfo.m_sDetStartTime = dtSearchStartTime.toString("hh:mm:ss");
	dataInfo.extraDataInfo.m_sDetEndTime = dtSearchEndTime.toString("hh:mm:ss");
	for (int i = 0; i < vecRecordSese.size(); i++)
	{
		QDateTime dtAlmNow = vecRecordSese[i].value("ALARM_TIME").toDateTime();
		//需要根据配置的筛选时间筛选,比如60分钟之内只能有一条报警
		if (0 == i)//第一个报警是需要的
		{
			m_dtPrevSeseAlm = dtAlmNow;
			dataInfo.extraDataInfo.m_nAlmCount += 1;
		}
		else//往后的报警进行比较
		{
			// 计算时间差（秒）
			int nSecDif = dtAlmNow.secsTo(m_dtPrevSeseAlm);
			if (nSecDif < 3600)//指定分钟(比如60分钟)之内
			{
				continue;//本次报警直接过滤
			}
			else//超过时间，可以取新的报警了
			{
				m_dtPrevSeseAlm = dtAlmNow;
				dataInfo.extraDataInfo.m_nAlmCount += 1;
			}
		}
		SPdfSeSeSglAlmInfo pdfAlmInfo;
		pdfAlmInfo.m_sDevName = vecRecordSese[i].value("DEV_NAME").toString();
		pdfAlmInfo.m_nChannelNo = vecRecordSese[i].value("CHANNEL_NO").toInt();
		pdfAlmInfo.sAlmTime = vecRecordSese[i].value("ALARM_TIME").toDateTime().toString("hh:mm:ss");
		pdfAlmInfo.sAccDirect = vecRecordSese[i].value("ACC_DIRECT").toString();
		pdfAlmInfo.nTempAlm = vecRecordSese[i].value("TEMP_ALARM").toInt();
		pdfAlmInfo.nVibrationAlm = vecRecordSese[i].value("VIBRATION_ALARM").toInt();
		pdfAlmInfo.nVoiceAlm = vecRecordSese[i].value("VOICE_ALARM").toInt();
		pdfAlmInfo.m_dTempVal = vecRecordSese[i].value("TEMP_VALUE").toDouble();
		pdfAlmInfo.m_sBeltName = vecRecordSese[i].value("BELT_NAME").toString();
		pdfAlmInfo.m_dRms = vecRecordSese[i].value("RMS").toDouble();
		pdfAlmInfo.m_sChannelName = vecRecordSese[i].value("CHANNEL_NAME").toString();

		dataInfo.extraDataInfo.m_vecFormSeSeAlmInfo.push_back(pdfAlmInfo);
	}
	//组包完成后，将生成报告的记录插入到数据库中
	{
		//去查询报警记录表
		QVector<QSqlRecord> vecInfo;
		QString sErrMsg = "";
		QString sPath = sOutPath.remove(sPdfFolderPath);
		QString sPdfPathUrl = sHttpPrefix + sPath;
		QString sRobPdfPathUrl = sPdfPathUrl.replace("#", "%23");//URL路径
		CSqlOper::instance().insertResult(createGuid(), 2, sCurrTime + A2T("声振温检测报告.pdf"), dtSearchEndTime, dataInfo.extraDataInfo.m_sOutputPdfFolder, sRobPdfPathUrl, sErrMsg);
	}
	return true;
}
